<template>
	<tm-app ref="app">
		<tm-navbar title="优惠卡" color="orange" linear="right" beforeBack="false" hideHome="false"
			@click="backPage"></tm-navbar>
		
		<tm-result v-if="!resultFlag" color="orange" :size="280" :title="title" :subTitle="subTitle" status="success" @click="backMyPage()"></tm-result>
		
		
		<tm-coupon v-if="resultFlag" color="orange" :priceDetail="yk" :rightDetail="ykt" linear="right" linear-deep="accent"
			mainColor="orange" font-color="" btnLabel="立即购买" @click="payYkMoney()"
			style="margin-top: 20rpx;"></tm-coupon>
		<tm-coupon v-if="resultFlag" color="orange" :priceDetail="jk" :rightDetail="jkt" linear="right" linear-deep="accent"
			mainColor="orange" font-color="" btnLabel="立即购买" @click="payJkMoney()" style="margin-top: 20rpx;"></tm-coupon>
		<tm-coupon v-if="resultFlag" color="orange" :priceDetail="nk" :rightDetail="nkt" linear="right" linear-deep="accent"
			mainColor="orange" font-color="" btnLabel="立即购买" @click="payBnkMoney()" style="margin-top: 20rpx;"></tm-coupon>
	</tm-app>
</template>


<script lang="ts" setup>
	import { ref, nextTick, toRefs, reactive } from "vue";
	import { onLoad } from "@dcloudio/uni-app";
	import { isEmpty } from "@/tmui/tool/function/util";
	import { payYhkqOrder, getDetailBywxid } from '@/api/jz';

	const userid = ref("");
	const resultFlag = ref(false);
	const title = ref("您已是我们的会员！");
	const subTitle = ref("享受**折优惠，到期日期**！");

	onLoad(() => {
		userid.value = uni.getStorageSync("userid");
		// console.log('初始化程序加载数据！', userid.value);
		if (isEmpty(userid.value)) {
			uni.navigateTo({
				url: '/pages/auth/auth'
			})
			return null;
		}
		// 进行数据校验 判断当前学员是否已经绑定过
		loadJzData();
		return null;
	})
	
	function backMyPage(){
		uni.navigateTo({
			url: '/pages/index/my'
		})
	}

	const loadJzData = async() => {
		let param = {"wxid":userid.value}
		const result = await getDetailBywxid(param);
		console.log(JSON.stringify(result));
		if(result.qstatus === "1"){
			resultFlag.value = false;
			subTitle.value = `享受${result.zk}折优惠，到期日期${result.endtime}.`;
		}else{
			resultFlag.value = true;
		}
	}


	function backPage(e : any) {
		uni.redirectTo({
			url: '/pages/index/my'
		})
	}
	
	const payBnkMoney = async() => {
		let param = { "id": "17", "wxid": userid.value };
		const result = await payYhkqOrder(param);
		uni.requestPayment({
			provider: 'wxpay',    // 支付类型-固定值
			timeStamp: result.timeStamp, // 时间戳（单位：秒）
			nonceStr: result.nonceStr, // 随机字符串
			package: result.packageValue, // 固定值
			signType: result.signType, // 固定值
			paySign: result.paySign, // 签名
			success: (res) => {
				uni.redirectTo({
					url: '/pages/my/yhk'
				})
			},
			fail: (err) => {
		
			}
		});
	}
	
	const payJkMoney = async() => {
		let param = { "id": "16", "wxid": userid.value };
		const result = await payYhkqOrder(param);
		uni.requestPayment({
			provider: 'wxpay',    // 支付类型-固定值
			timeStamp: result.timeStamp, // 时间戳（单位：秒）
			nonceStr: result.nonceStr, // 随机字符串
			package: result.packageValue, // 固定值
			signType: result.signType, // 固定值
			paySign: result.paySign, // 签名
			success: (res) => {
				uni.redirectTo({
					url: '/pages/my/yhk'
				})
			},
			fail: (err) => {
		
			}
		});
	}
	

	const payYkMoney = async () => {
		let param = { "id": "15", "wxid": userid.value };
		const result = await payYhkqOrder(param);
		uni.requestPayment({
			provider: 'wxpay',    // 支付类型-固定值
			timeStamp: result.timeStamp, // 时间戳（单位：秒）
			nonceStr: result.nonceStr, // 随机字符串
			package: result.packageValue, // 固定值
			signType: result.signType, // 固定值
			paySign: result.paySign, // 签名
			success: (res) => {
				uni.redirectTo({
					url: '/pages/my/yhk'
				})
			},
			fail: (err) => {

			}
		});



	}




	// -----------------------------------进行月卡、季卡、年卡的权限------------------------------------------
	interface surplice {
		price : number | string,// 价格金额
		suffix : string,// 后缀文本
		prefix : string,// 前缀文本
		subtext : string// 小文本
	}

	interface rightSurplice {
		title : number | string,// 标题
		subtitle : string,// 副标题
		time : string,// 有效期时间文本
	}
	const ykq = reactive({
		obj: {
			price: "￥39.9",// 价格金额
			suffix: "",// 后缀文本
			prefix: "",// 前缀文本
			subtext: "享受94折优惠"// 小文本
		}
	})
	const yk : surplice = ykq.obj;
	const ykTitle = reactive({
		obj: {
			title: "月卡",
			subtitle: "有效期30天",
			time: "从支付之日起开始计算"
		}
	})
	const ykt : rightSurplice = ykTitle.obj;

	const jkq = reactive({
		obj: {
			price: "￥79.9",// 价格金额
			suffix: "",// 后缀文本
			prefix: "",// 前缀文本
			subtext: "享受92折优惠"// 小文本
		}
	})
	const jk : surplice = jkq.obj;
	const jykTitle = reactive({
		obj: {
			title: "季卡",
			subtitle: "有效期90天",
			time: "从支付之日起开始计算"
		}
	})
	const jkt : rightSurplice = jykTitle.obj;

	const nkq = reactive({
		obj: {
			price: "￥149.9",// 价格金额
			suffix: "",// 后缀文本
			prefix: "",// 前缀文本
			subtext: "享受9折优惠"// 小文本
		}
	})
	const nk : surplice = nkq.obj;
	const nykTitle = reactive({
		obj: {
			title: "半年卡",
			subtitle: "有效期180天",
			time: "从支付之日起开始计算"
		}
	})
	const nkt : rightSurplice = nykTitle.obj;
</script>